package me.zhengjie.modules.common.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import me.zhengjie.modules.common.service.ExcelProcessor;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HSSFProcessorImpl implements ExcelProcessor {
	private HSSFWorkbook workbook ;
	private HSSFSheet worksheet ;

	public HSSFProcessorImpl(String fileName, Integer index) {
		FileInputStream fis = null ;
		try{
			fis = new FileInputStream(fileName);
			workbook = new HSSFWorkbook(fis);
			worksheet = workbook.getSheetAt(index) ;
		} catch(IOException ex){
			ex.printStackTrace();
		}finally {
			if(fis != null) {
				try {
					fis.close();
				}catch(IOException ex){
				}
			}
		}
	}


	@Override
	public String getCellStringValue(Object obj) {
		if(obj==null)
			return "" ;
		HSSFCell cell = (HSSFCell)obj ;
		switch (cell.getCellTypeEnum()) {
			case FORMULA:
				return cell.getCellFormula();

			case NUMERIC:
				DecimalFormat decimalFormat = new DecimalFormat("#");
				return decimalFormat.format(cell.getNumericCellValue());

            /*case STRING:
                value = "STRING value=" + cell.getStringCellValue();
                break;*/
			default:
				return cell.getStringCellValue() ;
		}
	}

	@Override
	public String getCellStringValueEx(Object obj) {
		if(obj==null)
			return "" ;
		HSSFCell cell = (HSSFCell)obj ;
		DecimalFormat decimalFormat = new DecimalFormat("#.#");
		switch (cell.getCellTypeEnum()) {
			case FORMULA:
				return decimalFormat.format(cell.getNumericCellValue());

			case NUMERIC:
				return decimalFormat.format(cell.getNumericCellValue());

            /*case STRING:
                value = "STRING value=" + cell.getStringCellValue();
                break;*/
			default:
				return cell.getStringCellValue() ;
		}
	}

	@Override
	public String getCellStringValueExEx(Object obj) {
		if(obj == null)
			return "" ;

		HSSFCell cell = (HSSFCell)obj ;
		DecimalFormat decimalFormat = new DecimalFormat("#.##");
		switch (cell.getCellTypeEnum()) {
			case FORMULA:
				try {
					if(cell.getCellStyle().getDataFormatString().contains("%")) {
						decimalFormat = new DecimalFormat("#.####");
						return decimalFormat.format(cell.getNumericCellValue()*100);
					}
					return decimalFormat.format(cell.getNumericCellValue());
				}catch(Exception ex) {
					return "";
				}

			case NUMERIC:
				return decimalFormat.format(cell.getNumericCellValue());

            /*case STRING:
                value = "STRING value=" + cell.getStringCellValue();
                break;*/
			default:
				return cell.getStringCellValue() ;
		}
	}

	@Override
	public String getTabName(int index) {
		return this.workbook.getSheetAt(index).getSheetName() ;
	}

	@Override
	public List<ArrayList<Object>> parseRows(int index) {
		this.worksheet = this.workbook.getSheetAt(index);
		return parseRows();
	}

	@Override
	public List<ArrayList<Object>> parseRows() {
		int maxCols = -1 ;
		List<ArrayList<Object>> rowItems = new ArrayList<ArrayList<Object>>();

		int rows = worksheet.getPhysicalNumberOfRows();
		for (int r = 0; r < rows; r++) {
			HSSFRow row = worksheet.getRow(r);
			if (row == null) {
				continue;
			}
			int cells = row.getPhysicalNumberOfCells();
			if(cells > maxCols)
				maxCols = cells ;

			List<Object> colItems = new ArrayList<Object>();
			for (int c = 0; c < maxCols; c++) {
				HSSFCell cell = row.getCell(c);
				colItems.add(cell);
			}
			rowItems.add((ArrayList<Object>)colItems) ;
		}
		return rowItems ;
	}

	@Override
	public Double getNumericCellValue(Object obj) {
		if(obj == null)
			return 0d ;
		else{
			HSSFCell cell = (HSSFCell)obj ;
			if(cell.getCellTypeEnum() != CellType.NUMERIC ) {
				try {
					return Double.valueOf(cell.getStringCellValue());
				}catch(Exception ex){
					//ex.printStackTrace();
					return 0d ;
				}
			}
			else
				return ((HSSFCell)obj).getNumericCellValue() ;
		}
	}

	@Override
	public int getTabCount() {
		return this.workbook.getNumberOfSheets() ;
	}
}
